package guo.xungengauth.controller;

import guo.xungengauth.feign.UserFeign;
import guo.xungengauth.utils.AccessPrivider;
import guo.xungengauth.vo.PrividerToken;
import guo.xungengauth.vo.User;
import guo.xungengcommon.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class GiteeLogin {
    @Autowired
    private PrividerToken prividerToken;

    @Autowired
    private AccessPrivider accessPrivider;

    @Autowired
    UserFeign userFeign;

    @Autowired
    private User user;

    @GetMapping("/callback")
    public String callback(@RequestParam(name = "code") String code,
                           @RequestParam(name = "state") String state,
                           HttpSession session) {


        prividerToken.setClient_id("6ac3b403c36e81087f1798edb0fee6fc18bfb1cdc03bee9eb56b010e615446b7");
        prividerToken.setCode(code);
        prividerToken.setRedirect_uri("http://auth.xungeng.com/callback");
        prividerToken.setState(state);
        prividerToken.setClient_Secret("de24e1dd05f06bcb581b2048257259bbce1465ae9010da0ef25e1961fde9b25b");
        String Token = accessPrivider.getToken(prividerToken);
        user = accessPrivider.getUser(Token);

        UserVo userVo = new UserVo();
        userVo.setEmail(user.getEmail());
        userVo.setUserName(user.getName());
        userVo.setToken(Token);
        userVo.setRealName(user.getLogin());
        userVo.setGiteeId(user.getId());

        userFeign.saveGiteeUser(userVo);
        session.setAttribute("user",userVo);

        return "redirect:http://main.xungeng.com/index";
    }
}
